package com.zhike.b2b.search;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.FSDirectory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.wltea.analyzer.lucene.IKAnalyzer;
import org.wltea.analyzer.lucene.IKQueryParser;
import org.wltea.analyzer.lucene.IKSimilarity;

import com.saas.biz.paramMgr.ParamInfo;
import com.saas.sys.exp.SaasApplicationException;

public class IndexerSearchFile
{
	Logger log = LoggerFactory.getLogger(IndexerSearchFile.class);
	
  private static ParamInfo param;

  public IndexerSearchFile()
  {
    param = new ParamInfo();
  }

  public ArrayList getSearch(String str1, String paramString2)
    throws IOException, ParseException, SaasApplicationException
  {
    ArrayList localArrayList = new ArrayList();
    String str2 = param.getParameter("mysqlbase.rootpath");
    File inxFile = new File(str2 + "/upload/departmentindex/" + paramString2 + "/index");
    FSDirectory fsdir = FSDirectory.open(inxFile);
    IndexSearcher searcher = new IndexSearcher(fsdir);
    searcher.setSimilarity(new IKSimilarity());
    //实例化IKAnalyzer分词器
    Analyzer analyzer = new IKAnalyzer();
    Query query = IKQueryParser.parse("content",str1);
    if (searcher != null)
    {
      TopScoreDocCollector collector = TopScoreDocCollector.create(100,false);
      searcher.search(query,collector);
      ScoreDoc[] docs = collector.topDocs().scoreDocs;
      if (docs.length > 0)
        for (int i = 0; i < docs.length; i++)
        {
    	  int inx = docs[i].doc;
          Document doc = searcher.doc(inx);
         
          HashMap localHashMap = new HashMap();
          localHashMap.put("path", doc.get("path"));
          localHashMap.put("title", doc.get("title"));
          localHashMap.put("type", doc.get("type"));
          localHashMap.put("content", doc.get("content"));
          localArrayList.add(localHashMap);
        }
      else
        log.debug("0个结果!");
    }
    return localArrayList;
  }

  public ArrayList getSearchtype(String keyword, String paramString2)
    throws IOException, ParseException, SaasApplicationException
  {
    ArrayList localArrayList = new ArrayList();
    String str2 = param.getParameter("mysqlbase.rootpath");
    File inxFile = new File(str2 + "/upload/departmentindex/" + paramString2 + "/index");
    FSDirectory fsdir = FSDirectory.open(inxFile);
    IndexSearcher searcher = new IndexSearcher(fsdir);
    searcher.setSimilarity(new IKSimilarity());
    //实例化IKAnalyzer分词器
    Analyzer analyzer = new IKAnalyzer();
    Query query = IKQueryParser.parse("type",keyword);
    if (searcher != null)
    {
		TopScoreDocCollector collector = TopScoreDocCollector.create(100,false);
	    searcher.search(query,collector);
	    ScoreDoc[] docs = collector.topDocs().scoreDocs;
      if (docs.length > 0)
      {
        for (int i = 0; i < docs.length; i++)
        {
    	 int inx = docs[i].doc;
         Document doc = searcher.doc(inx);
         HashMap hm = new HashMap();
         hm.put("path", doc.get("path"));
          hm.put("title", doc.get("title"));
          hm.put("type", doc.get("type"));
          hm.put("content", doc.get("content"));
          localArrayList.add(hm);
        }
      }
      else
        log.debug("0个结果!");
    }
    return localArrayList;
  }
}

